﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIMS.Entities;
using LIMS.Shared;

namespace LIMS.Windows
{
    public partial class HODepartmentsPopup : Form
    {
        private Boolean IsFormLoading = true;
        public User oUser = new User();
        private string sDepartmentID = string.Empty;
        private int iUserType = 0;
        public string DepartmentID
        {
            get { return sDepartmentID; }
            set { sDepartmentID = value; }
        }

        public HODepartmentsPopup(User p_oUser, int p_UserType)
        {
            InitializeComponent();
        }

        private void HODepartmentsPopup_Load(object sender, EventArgs e)
        {
            List<Site> oSiteList = new List<Site>();

            oSiteList = SharedResource.GetSites(oUser, false);

            chkListSite.DataSource = oSiteList;
            chkListSite.ValueMember = "Id";
            chkListSite.DisplayMember = "Name";

            IsFormLoading = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string sDeptId = string.Empty;
            //foreach (int indexChecked in chkListDept.CheckedIndices)
            //{
            //    sDeptId = sDeptId + indexChecked;
            //}
            foreach (Object item in chkListDept.CheckedItems)
            {

                sDeptId = sDeptId + ((Department)item).Id + ",";
            }
            DepartmentID = sDeptId.Substring(0, sDeptId.Length - 1);
            this.Close();
        }

        private void chkListSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (IsFormLoading) { return; }

                string sSiteId = string.Empty;

                foreach (Object item in chkListSite.CheckedItems)
                {

                    sSiteId = sSiteId + ((Site)item).Id + ",";
                }
                if (sSiteId.Length > 0)
                {
                    sSiteId = sSiteId.Substring(0, sSiteId.Length - 1);

                    chkListPlant.DataSource = null;
                    chkListDept.DataSource = null;

                    List<Plant> oPlantName = new List<Plant>();
                    oPlantName = SharedResource.GetPlants(sSiteId, false);

                    chkListPlant.DataSource = oPlantName;
                    chkListPlant.ValueMember = "Id";
                    chkListPlant.DisplayMember = "Name";
                }
                else
                {
                    chkListPlant.DataSource = null;
                    chkListDept.DataSource = null;

                }
            }
            catch (Exception ex)
            { }
        }

        private void chkListPlant_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string sPlantId = string.Empty;

                foreach (Object item in chkListPlant.CheckedItems)
                {

                    sPlantId = sPlantId + ((Plant)item).Id + ",";
                }
                if (sPlantId.Length > 0)
                {
                    sPlantId = sPlantId.Substring(0, sPlantId.Length - 1);

                    chkListDept.DataSource = null;
                    List<Department> oDeptName = new List<Department>();

                    oDeptName = SharedResource.GetDepartmentByPlant(sPlantId, false);

                    chkListDept.DataSource = oDeptName;
                    chkListDept.ValueMember = "Id";
                    chkListDept.DisplayMember = "Name";
                }
                else
                {
                    chkListDept.DataSource = null;
                }
            }
            catch (Exception ex)
            { }
        }

    }
}
